<?php

includePackage('Eip');
class ContactWebModule extends WebModule
{
    protected static $defaultModel = 'EipDataModel';
    protected $id = 'contact';

    protected function initialize() {
    }

    protected function getUnitsFromEip($unit = '') {
        $controller = DataModel::factory('EipDataModel', array('RETRIEVER_CLASS' => 'DatabaseEipRetriever', 'type' => 'unit', 'DB_TABLE' => 'units', 'DB_TYPE' => 'mysql', 'DB_HOST' => '127.0.0.1', 'DB_USER' => '[YOUR_USERNAME]', 'DB_PASS' => '[YOUR_PASSWORD]', 'DB_DBNAME' => 'ldap', 'param' => array('unit' => $unit)));

        return $controller;
    }

    protected function getSearchFromEip($searchTerms = '') {
        $controller = DataModel::factory('EipDataModel', array('RETRIEVER_CLASS' => 'DatabaseEipRetriever', 'type' => 'search', 'DB_TABLE' => 'units', 'DB_TYPE' => 'mysql', 'DB_HOST' => '127.0.0.1', 'DB_USER' => '[YOUR_USERNAME]', 'DB_PASS' => '[YOUR_PASSWORD]', 'DB_DBNAME' => 'ldap', 'param' => array('searchTerms' => $searchTerms)));

        return $controller;
    }

    private function getPersonlistFromEip($schname = '', $prin = 0) {
        $user = $this->getUser();
        $controller = DataModel::factory('EipDataModel', array('RETRIEVER_CLASS' => 'UrlEipRetriever', 'TYPE' => 'personlist', 'BASE_URL' => '[YOUR_SERVER]', 'ARGS' => array('schname' => $schname, 'prin' => $prin, 'quser' => $user->getEmail())));

        return $controller;
    }

    protected function pushMsg($schname, $msg) {
        $controller = DataModel::factory('EipDataModel', array('RETRIEVER_CLASS' => 'UrlEipRetriever', 'CACHE_LIFETIME' => 0, 'TYPE' => 'push', 'BASE_URL' => '[YOUR_SERVER]', 'ARGS' => array('schname' => $schname, 'msg' => $msg)));
        $result = $controller->items();

        return $result;
    }

    protected function initializeForPage() {
        $user = $this->getUser();
        $identity = $user->getAttribute('identity');
        $position = $user->getAttribute('position');
        $unit = $user->getAttribute('unit');

        switch($this->page) {
            case 'index':
                if ($unit == '教育處' || $position == '校長') {
                } elseif ($identity == '教師' || $identity == '學生') {
                    $this->redirectTo('detail');
                } else {
                    $this->unauthorizedAccess();
                }

                $units = array();
                $controller = $this->getUnitsFromEip();
                $items = $controller->items();
                foreach($items as $item) {
                    $unit = array();
                    $unit['title'] = $item['area'];
                    $unit['url'] = $this->buildBreadcrumbURL('unit', array('unit' => $item['area']), true);
                    $units[] = $unit;
                }
                $this->assign('units', $units);
                break;
            case 'search':
                $searchTerms = $this->getArg('filter');
                $this->assign('searchTerms', $searchTerms);

                $schools = array();
                $controller = $this->getSearchFromEip($searchTerms);
                $items = $controller->items();
                foreach($items as $item) {
                    $school = array();
                    $school['title'] = $item['name'];
                    $school['url'] = $this->buildBreadcrumbURL('detail', array('name' => $item['name']), true);
                    $schools[] = $school;
                }
                $this->assign('schools', $schools);
                break;
            case 'unit':
                $unit = $this->getArg('unit', '');

                $schools = array();
                $controller = $this->getUnitsFromEip($unit);
                $items = $controller->items();
                foreach($items as $item) {
                    $school = array();
                    $school['title'] = $item['name'];
                    $school['url'] = $this->buildBreadcrumbURL('detail', array('name' => $item['name']), true);
                    $schools[] = $school;
                }
                $this->assign('schools', $schools);
                break;
            case 'detail':
                $pushButton = false;
                if ($unit == '教育處' || $position == '校長') {
                    $schoolName = $this->getArg('name', '');
                    if ($unit == $schoolName)
                        $pushButton = true;
                    $prin = 1;
                } elseif ($identity == '教師' || $identity == '學生') {
                    $schoolName = $unit;
                    $prin = 0;
                } else {
                    $this->unauthorizedAccess();
                }

                $personDetails = array();
                $controller = $this->getPersonlistFromEip($schoolName, $prin);
                $items = $controller->items();
                foreach($items as $item) {
                    list($title, $name, $tel, $ext, $mobile, $other) = explode(',', $item);
                    $personDetails[$title] = array(
                        array('label' => '姓名', 'title' => $name),
                        array('label' => '電話', 'title' => $tel . (($ext) ? '#' . $ext : ''), 'url' => PhoneFormatter::getPhoneURL($tel . (($ext) ? '#' . $ext : ''))),
                        array('label' => '手機', 'title' => $mobile, 'url' => PhoneFormatter::getPhoneURL($mobile)),
                     );
                }

                $this->assign('schoolName', $schoolName);
                $this->assign('pushButton', $pushButton);
                $this->assign('personDetails', $personDetails);
                break;
            case 'push':
                if ($unit <> '教育處' && $position <> '校長') {
                    $this->unauthorizedAccess();
                }
                break;
            case 'result':
                if ($unit <> '教育處' && $position <> '校長') {
                    $this->unauthorizedAccess();
                }
                $schname = $unit;
                $msg = $this->getArg('msg', '');
                $result = $this->pushMsg($schname, $msg);
                break;
        }
    }
}
